package com.yixuan.mt.core.response;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

/**
 * 固定的响应, 用于复用
 */
public class FixedResponse {

    public final static FullHttpResponse okResponse = generate(HttpResponseStatus.OK);
    public final static FullHttpResponse methodNotAllowedResponse = generate(HttpResponseStatus.METHOD_NOT_ALLOWED);
    public final static FullHttpResponse fileAlreadyExistsResponse = generate(HttpResponseStatus.BAD_REQUEST, "文件路径已存在");
    public final static FullHttpResponse unauthorizedResponse = generate(HttpResponseStatus.UNAUTHORIZED);
    public final static FullHttpResponse forbiddenResponse = generate(HttpResponseStatus.FORBIDDEN);
    public final static FullHttpResponse notFoundResponse = generate(HttpResponseStatus.NOT_FOUND);
    public final static FullHttpResponse internalServerErrorResponse = generate(HttpResponseStatus.INTERNAL_SERVER_ERROR);

    public static void sendResponse(ChannelHandlerContext ctx, FullHttpResponse response) {
        response.content().retain();
        response.content().markReaderIndex();
        ctx.writeAndFlush(response);
        response.content().resetReaderIndex();
    }

    public static void sendOkResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, okResponse);
    }

    public static void sendMethodNotAllowedResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, methodNotAllowedResponse);
    }

    public static void sendFileAlreadyExistsResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, fileAlreadyExistsResponse);
    }


    public static void sendUnauthorizedResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, unauthorizedResponse);
    }

    public static void sendForbiddenResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, forbiddenResponse);
    }

    public static void sendNotFoundResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, notFoundResponse);
    }

    public static void sendInternalServerErrorResponse(ChannelHandlerContext ctx) {
        sendResponse(ctx, internalServerErrorResponse);
    }

    private static FullHttpResponse generate(HttpResponseStatus status) {
        FullHttpResponse response;
        if (200 <= status.code() && status.code() < 300) {
            response = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    status,
                    Unpooled.copiedBuffer("{\"code\":1,\"data\":null,\"msg\":\"" + status.reasonPhrase() + "\"}", CharsetUtil.UTF_8)
            );
        } else {
            response = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    status,
                    Unpooled.copiedBuffer("{\"code\":0,\"data\":null,\"msg\":\"" + status.reasonPhrase() + "\"}", CharsetUtil.UTF_8)
            );
        }

        // 设置响应头信息
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());

        return response;
    }

    private static FullHttpResponse generate(HttpResponseStatus status, String msg) {
        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                status,
                Unpooled.copiedBuffer("{\"code\":0,\"data\":null,\"msg\":\"" + msg + "\"}", CharsetUtil.UTF_8)
        );

        // 设置响应头信息
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/json; charset=UTF-8");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());

        return response;
    }
}